<?php
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private',false);
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.$this->filename.'.csv"' );
header('Content-Transfer-Encoding: binary');

$fh = fopen("php://output", 'w');

/*
 * Output report headings
 */
foreach($this->headings as $heading)
    fputcsv($fh, array($heading));

/*
 * Output table headers
 */
$ln = array();
foreach($this->headers as $header)
    $ln[] = $header;
fputcsv($fh, $ln);

/*
 * Output table data
 */
$cols = array_keys(get_object_vars($this->headers));
foreach($this->data as $row) {
    $ln = array();
    foreach($cols as $col)
        $ln[] = isset($row->$col) ? $row->$col : '';
    fputcsv($fh, $ln);
}

/*
 * Output report footnotes
 */
foreach($this->footnotes as $footnote)
    fputcsv($fh, array($footnote));

fclose($fh);
exit;
?>